﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows;
using System.Collections.Generic;

namespace Transportr
{
    public class LookaroundModel : BaseItemViewModel
    {
        private string mStatus;

        public ObservableCollection<StationItemViewModel> Items { get; private set; }
        public string FilterText { get; set; }
    
        public ObservableCollection<StationItemViewModel> FilteredItems
        {
            get
            {
                ObservableCollection<StationItemViewModel> ret = new ObservableCollection<StationItemViewModel>();
                foreach (StationItemViewModel i in Items)
                {
                    if((FilterText.Length == 0 || i.LineNumber.Contains(FilterText))
                        && i.StationName.CompareTo(i.EndStationName) != 0)
                        ret.Add(i);
                }
                return ret;
            }
        }

        public bool IsDataLoaded { get; private set; }

        public string Status
        {
            get { return mStatus; }
            set
            {
                if (value != mStatus)
                {
                    mStatus = value;
                    NotifyPropertyChanged("Status");
                }
            }
        }

        public bool StatusVisible { get { return this.mStatus.Length > 0; } }

        public LookaroundModel()
        {
            this.FilterText = "";
            this.Status = "";
            this.Items = new ObservableCollection<StationItemViewModel>();

#if WINDOWS_PHONE
            this.Items.CollectionChanged += (object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) =>
                {
                    NotifyPropertyChanged("FilteredItems");
                };
#endif
            if (!LocationModel.Valid)
            {
                Status = "No location available.";
            }
            else
            {
                Status = String.Format("Your location: {0}; {1}", LocationModel.CurrentLocation.Latitude, LocationModel.CurrentLocation.Longitude);
                this.BeginUpdate();
            }
        }

        public void BeginUpdate()
        {
            IsDataLoaded = false;
            if (LocationModel.Valid)
            {
                Service.SendRequest(ServiceModel.Function.StationLines, new ServiceModel.Parameters()
                    {
                        stLat = LocationModel.CurrentLocation.Latitude,
                        stLng = LocationModel.CurrentLocation.Longitude,
                        dist = 1
                    }, EndUpdate, false);
            }
        }

        void EndUpdate(object sender, Service.RequestState state)
        {
#if WINDOWS_PHONE
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {
#endif
                    this.Items.Clear();
                    List<StationItemViewModel> temp = new List<StationItemViewModel>();
                    foreach (ServiceModel.StationLine sl in state.Response.StationLines.StationLine)
                    {
                        temp.Add(new StationItemViewModel()
                        {
                            RawData = sl
                        });
                    }
                    temp.Sort((a, b) => (int)(a.Distance * 1000 - b.Distance * 1000));
                    foreach (StationItemViewModel i in temp) this.Items.Add(i);
                    this.IsDataLoaded = true;
#if WINDOWS_PHONE
                });
#endif
        }
    }
}
